package com.juyu.miliao.api.dao.redis;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.constant.UpdateUserRedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import com.juyu.miliao.api.dao.mysql.UserInfoDao;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.util.RedisKeys;


@Repository
public class UserRedisDao {
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	@Autowired
	private UserInfoDao userInfoDao;
	public Optional<Integer> getUserIdByNickName(String nickName) {
		Object o = stringRedisTemplate.opsForHash().get(RedisKeys.HASH_NICKNAME.getValue(), nickName);
		Integer userId = null;
		if (o != null) {
			userId = Integer.parseInt(o.toString());
		}
		return Optional.ofNullable(userId);
	}

	public void addUserIdAndNickName(int userId, String nickname) {
		stringRedisTemplate.opsForHash().put(RedisKeys.HASH_NICKNAME.getValue(), nickname, userId + "");
	}
	
	public void replaceNickname(User entity) {
		User userInfoById = userInfoDao.getUserInfoById(entity.getUserId());
		stringRedisTemplate.opsForHash().delete(RedisKeys.HASH_NICKNAME.getValue(), userInfoById.getNickName());
		stringRedisTemplate.opsForHash().put(RedisKeys.HASH_NICKNAME.getValue(), entity.getNickName(), entity.getUserId() + "");
	}
	//用户信息审核 nickname 主播
	public void userInfoCheckNicNameForHost(long userId,String nickName){
		JSONObject json = new JSONObject();
		json.put("userId",userId);
		json.put("nickName",nickName);
		stringRedisTemplate.opsForHash().put(UpdateUserRedisKey.NICKNAME_CHECK_HOST_HASH,userId+"",json.toString());
		stringRedisTemplate.opsForZSet().add(UpdateUserRedisKey.NICKNAME_CHECK_HOST_ZSER,String.valueOf(userId),LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
	}
	//用户信息审核 nickname 用户
	public void userInfoCheckNicName(long userId){
		stringRedisTemplate.opsForZSet().add(UpdateUserRedisKey.NICKNAME_CHECK_SER,String.valueOf(userId),LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
	}
	//用户信息审核 Signature 用户
	public void userInfoCheckSignature(long userId){
		stringRedisTemplate.opsForZSet().add(UpdateUserRedisKey.SIGNATURE_CHECK_SER,String.valueOf(userId),LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
	}
	//用户信息审核 Signature 主播
	public void userInfoCheckSignatureForHost(long userId){
		stringRedisTemplate.opsForZSet().add(UpdateUserRedisKey.SIGNATURE_CHECK_HOST_ZSER,String.valueOf(userId),LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
	}
	//用户信息审核 icon 用户
	public void userInfoCheckIcon(long userId){
		stringRedisTemplate.opsForZSet().add(UpdateUserRedisKey.ICON_CHECK_SER,String.valueOf(userId),LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
	}
	//用户信息审核 icon 主播
	public void userInfoCheckIconForHost(long userId,String icon){
		JSONObject json = new JSONObject();
		json.put("userId",userId);
		json.put("icon",icon);
		stringRedisTemplate.opsForHash().put(UpdateUserRedisKey.ICON_CHECK_HOST_HASH,userId+"",json.toString());
		stringRedisTemplate.opsForZSet().add(UpdateUserRedisKey.ICON_CHECK_HOST_ZSER,String.valueOf(userId),LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
	}
	//用户信息审核 priUrl 用户
	public void userInfoCheckPriUrl(long privateId){
		stringRedisTemplate.opsForZSet().add(UpdateUserRedisKey.PRIURL_CHECK_SER,String.valueOf(privateId),LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
	}
	//用户信息审核 priUrl 主播
	public void userInfoCheckPriUrlForHost(long privateId){
		stringRedisTemplate.opsForZSet().add(UpdateUserRedisKey.PRIURL_CHECK_HOST_ZSER,String.valueOf(privateId),LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
	}
	//用户信息审核 priUrl 用户
	public void deleteUserCheckPriUrl(long privateId){
		stringRedisTemplate.opsForZSet().remove(UpdateUserRedisKey.PRIURL_CHECK_SER,String.valueOf(privateId));
	}
	//用户信息审核 priUrl 主播
	public void deleteUserCheckPriUrlForHost(long privateId){
		stringRedisTemplate.opsForZSet().remove(UpdateUserRedisKey.PRIURL_CHECK_HOST_ZSER,String.valueOf(privateId));
	}
	//在redis中添加未成年模式的密码
	public void addMinorModePassword(long userId ,String password){
		stringRedisTemplate.opsForValue().set(UpdateUserRedisKey.USER_MINORMODEPASSWORD_STRING+userId,password);
	}
	//在redis中获取未成年模式的密码
	public String getMinorModePassword(long userId){
		return  stringRedisTemplate.opsForValue().get(UpdateUserRedisKey.USER_MINORMODEPASSWORD_STRING+userId);
	}
	//在redis中删除未成年模式的密码
	public boolean delMinorModePassword(long userId){
		return stringRedisTemplate.delete(UpdateUserRedisKey.USER_MINORMODEPASSWORD_STRING+userId);
	}
	//在redis中添加语音介绍用于审核
	public void userInfoCheckVoice(long userId,String voiceSignature,String voiceSignatureTime){
		JSONObject json = new JSONObject();
		json.put("userId",userId);
		json.put("voiceSignature",voiceSignature);
		json.put("voiceSignatureTime",voiceSignatureTime);
		stringRedisTemplate.opsForHash().put(UpdateUserRedisKey.USER_INFO_CHECK_VOICE_SIGNATURE_HASH,userId+"",json.toString());
		stringRedisTemplate.opsForZSet().add(UpdateUserRedisKey.USER_INFO_CHECK_VOICE_SIGNATURE_ZSET,
				userId+"",LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
	}
	public void delUserInfoCheckVoice(long userId){
		stringRedisTemplate.opsForHash().delete(UpdateUserRedisKey.USER_INFO_CHECK_VOICE_SIGNATURE_HASH,userId+"");
		stringRedisTemplate.opsForZSet().remove(UpdateUserRedisKey.USER_INFO_CHECK_VOICE_SIGNATURE_ZSET,
				userId+"");
	}
	public Boolean getUserInfoCheckVoice(long userId){
				stringRedisTemplate.opsForZSet().rank(UpdateUserRedisKey.USER_INFO_CHECK_VOICE_SIGNATURE_ZSET, userId + "");
		 Boolean isHaveVoice = stringRedisTemplate.opsForHash().hasKey(UpdateUserRedisKey.USER_INFO_CHECK_VOICE_SIGNATURE_HASH, userId + "");
		return isHaveVoice;
	}


	//获取广告开关
	public String getAdvertisement(){
		return stringRedisTemplate.opsForValue().get(RedisKeys.AD_SWITCH_MANAGE.getValue());
	}
//	public void deleteUserCheckUserCoverUrl(long privateId){
//		stringRedisTemplate.opsForZSet().remove(UpdateUserRedisKey.PRIURL_CHECK_SER,String.valueOf(privateId));
//	}
	public void putCheckUserCoverUrl(long userId,String nickName,String userCoverUrl){
		JSONObject json = new JSONObject();
		json.put("userId",userId);
		json.put("nickName",nickName);
		json.put("userCoverUrl",userCoverUrl);
		stringRedisTemplate.opsForHash().put(UpdateUserRedisKey.USER_COVER_URL_CHECK_HASH,userId+"",json.toString());
		stringRedisTemplate.opsForZSet().add(UpdateUserRedisKey.USER_COVER_URL_CHECK_ZSET,
                userId+"",LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
	}
	//用户审核信息 封面 主播
    public void putCheckUserCoverUrlForHost(long userId,String nickName,String userCoverUrl){
        JSONObject json = new JSONObject();
        json.put("userId",userId);
		json.put("nickName",nickName);
        json.put("userCoverUrl",userCoverUrl);
        stringRedisTemplate.opsForHash().put(UpdateUserRedisKey.USER_INFO_CHECK_COVERURL_HOST_HASH,userId+"",json.toString());
        stringRedisTemplate.opsForZSet().add(UpdateUserRedisKey.USER_INFO_CHECK_COVERURL_HOST_ZSET,
                userId+"",LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
    }
	public boolean hasKeyCheckUserCoverUrl(long userId){
		return stringRedisTemplate.opsForHash().hasKey(UpdateUserRedisKey.USER_COVER_URL_CHECK_HASH,userId+"");
	}
	public void deleteCheckUserCoverUrl(long userId){
		stringRedisTemplate.opsForHash().delete(UpdateUserRedisKey.USER_COVER_URL_CHECK_HASH,userId+"");
	}

	//存储打电话的用户id  key为 接通主播的id
    public long userConnectAnchorYuYin(long userId ,long anchorId){
	   return stringRedisTemplate.opsForSet().add(UpdateUserRedisKey.CONNECT_ANCHOR_YUYIN + anchorId,String.valueOf(userId));
    }
    //获取储存redis中接通语音列表
	public Set<String> getUserConnectAnchorYuYin(long anchorId){
		return stringRedisTemplate.opsForSet().members(UpdateUserRedisKey.CONNECT_ANCHOR_YUYIN + anchorId);
	}
	//存储打视频的用户id  key为 接通主播的id
    public long userConnectAnchorShiPin(long userId ,long anchorId){
       return stringRedisTemplate.opsForSet().add(UpdateUserRedisKey.CONNECT_ANCHOR_VIDEO + anchorId,String.valueOf(userId));
    }
	//获取储存redis中接通语音列表
	public Set<String> getUserConnectAnchorShiPin(long anchorId){
		return stringRedisTemplate.opsForSet().members(UpdateUserRedisKey.CONNECT_ANCHOR_VIDEO + anchorId);
	}

	//获取在线普通用户
	public Set<String> findOnLineUserBySex(int sex,int page ,int pageSize){
		return stringRedisTemplate.opsForZSet().reverseRange(RedisKeys.ZSET_USER_SEX_SORT.getValue(sex),
				page,pageSize);
	}
	//存储用户与主播说话的次数
	public Double addUserChat(long userId ,long insertId,int count){
		 return stringRedisTemplate.opsForZSet().incrementScore(UpdateUserRedisKey.USER_CHAT_ZSET+userId,String.valueOf(insertId),count);
	}
	//存储用户与主播说话的次数
	public Double getUserChat(long userId ,long insertId){
		return stringRedisTemplate.opsForZSet().score(UpdateUserRedisKey.USER_CHAT_ZSET+userId,String.valueOf(insertId));
	}

    public boolean userCoverUrl(long id) {
		return stringRedisTemplate.opsForZSet().add(RedisKeys.ZSET_USER_COVER_URL_BATCH.getValue(),String.valueOf(id),
				LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
	}
	public boolean userCoverUrlToHost(long id) {
		return stringRedisTemplate.opsForZSet().add(RedisKeys.USER_COVER_URL_BATCH_HOST_ZSET.getValue(),String.valueOf(id),
				LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
	}
	public void addMinorMode(long userId,int minor_mode_switch){
		stringRedisTemplate.opsForValue().set(UpdateUserRedisKey.USER_MINORMODESWITCH_STRING+userId,String.valueOf(minor_mode_switch),10,TimeUnit.MINUTES);
	}
	public String getMinorMode(long userId){
		return stringRedisTemplate.opsForValue().get(UpdateUserRedisKey.USER_MINORMODESWITCH_STRING+userId);
	}
	public boolean hasKeyMinorMode(long userId){
		return stringRedisTemplate.hasKey(UpdateUserRedisKey.USER_MINORMODESWITCH_STRING+userId);
	}
	public void delMinorMode(long userId){
		stringRedisTemplate.delete(UpdateUserRedisKey.USER_MINORMODESWITCH_STRING+userId);
	}
	//添加vip用戶已修改昵称次数
	public void addNiceNameVIPNum(long userId,String time,String num){
		stringRedisTemplate.opsForValue().set(UpdateUserRedisKey.USER_UPDATE_NICENAME_STRING+time+":"+userId,num);
	}
	//添加普通用戶已修改昵称次数
	public void addNiceNameNum(long userId,String num){
		stringRedisTemplate.opsForValue().set(UpdateUserRedisKey.USER_UPDATE_NICENAME_STRING+userId,num);
	}
	//获取普通用户已修改昵称次数
	public String getNiceNameNum(long userId){
		return  stringRedisTemplate.opsForValue().get(UpdateUserRedisKey.USER_UPDATE_NICENAME_STRING+userId);
	}
	//获取vip用户已修改昵称次数
	public String getNiceNameVIPNum(long userId,String time){
		return  stringRedisTemplate.opsForValue().get(UpdateUserRedisKey.USER_UPDATE_NICENAME_STRING+time+":"+userId);
	}
	//判断普通用户已修改昵称次数是否存在
	public boolean isUpdateNiceName(long userId){
		return stringRedisTemplate.hasKey(UpdateUserRedisKey.USER_UPDATE_NICENAME_STRING+userId);
	}
	//判断vip用户已修改昵称次数是否存在
	public boolean isUpdateVIPNiceName(long userId,String time){
		return stringRedisTemplate.hasKey(UpdateUserRedisKey.USER_UPDATE_NICENAME_STRING+time+":"+userId);
	}
	//添加普通用戶抽奖次数
    public void addPrizeNum(long userId,String time,String num){
        stringRedisTemplate.opsForValue().set(UpdateUserRedisKey.USER_PRIZE_NUM_STRING+time+":"+userId,num);
    }
    //添加SVIP用戶抽奖次数
    public void addPrizeSVIPNum(long userId,String time,String num){
        stringRedisTemplate.opsForValue().set(UpdateUserRedisKey.USER_PRIZE_NUM_STRING+"svip:"+time+":"+userId,num);
    }
    //添加用戶到等级抽奖次数
    public void addUserLevelPrizeNum(long userId,String time,int level,String num){
        stringRedisTemplate.opsForValue().set(UpdateUserRedisKey.USER_PRIZE_NUM_STRING+level+":"+time+":"+userId,num);
    }
    //获取普通用戶抽奖次数
    public String getPrizeNum(long userId,String time){
        return  stringRedisTemplate.opsForValue().get(UpdateUserRedisKey.USER_PRIZE_NUM_STRING+time+":"+userId);
    }
    //获取SVIP用戶抽奖次数
    public String getPrizeSVIPNum(long userId,String time){
       return stringRedisTemplate.opsForValue().get(UpdateUserRedisKey.USER_PRIZE_NUM_STRING+"svip:"+time+":"+userId);
    }
    //获取用戶到等级抽奖次数
    public String getUserLevelPrizeNum(long userId,String time,int level){
      return  stringRedisTemplate.opsForValue().get(UpdateUserRedisKey.USER_PRIZE_NUM_STRING+level+":"+time+":"+userId);
    }
    //判断普通用戶抽奖次数是否存在
    public boolean isPrizeNum(long userId,String time){
       return stringRedisTemplate.hasKey(UpdateUserRedisKey.USER_PRIZE_NUM_STRING+time+":"+userId);
    }
    //判断SVIP用戶抽奖次数是否存在
    public boolean isPrizeSVIPNum(long userId,String time){
        return stringRedisTemplate.hasKey(UpdateUserRedisKey.USER_PRIZE_NUM_STRING+"svip:"+time+":"+userId);
    }
    //判断用戶到等级抽奖次数是否存在
    public boolean isUserLevelPrizeNum(long userId,String time,int level){
       return stringRedisTemplate.hasKey(UpdateUserRedisKey.USER_PRIZE_NUM_STRING+level+":"+time+":"+userId);
    }
	//判断用戶是否领取过是否存在
	public boolean isSearchForBeans(long userId,String time){
		return stringRedisTemplate.hasKey(UpdateUserRedisKey.USER_SEARCH_FOR_BEANS_STRING+time+":"+userId);
	}
	//获取用戶领取救济豆(寻豆)次数
	public String getSearchForBeans(long userId,String time){
		return  stringRedisTemplate.opsForValue().get(UpdateUserRedisKey.USER_SEARCH_FOR_BEANS_STRING+time+":"+userId);
	}
	//添加用戶已领取救济豆(寻豆)次数
	public void addSearchForBeansNum(long userId,String time,String num){
		stringRedisTemplate.opsForValue().set(UpdateUserRedisKey.USER_SEARCH_FOR_BEANS_STRING+time+":"+userId,num);
	}
	//存储用户新手引导领取寻豆记录
	public void saveGiveUserBeans(long userId,long beans){
		stringRedisTemplate.opsForValue().set(UpdateUserRedisKey.NEW_BIE_GUIDE_GIVE_BEANS+userId,beans+"");
	}
	//判断用户是否领取过
	public boolean isGiveUserBeans(long userId){
		return stringRedisTemplate.hasKey(UpdateUserRedisKey.NEW_BIE_GUIDE_GIVE_BEANS+userId);
	}

    public void addUserCashEveryDayNumber(long userId) {
		stringRedisTemplate.opsForSet().add("userCashEveryDayNumber",userId+"");
    }
    public Boolean isMemberUserCashEveryDayNumber(long userId){
		Boolean temp = stringRedisTemplate.opsForSet().isMember("userCashEveryDayNumber",userId+"");
		if(temp == null){
			return false;
		}
		return temp;
	}

	/** 增加首冲用户*/
	public void addFirstRechargeUser(long userId){
		stringRedisTemplate.opsForSet().add("firstRechargeUser",userId+"");
	}
	public Boolean isMemberFirstRechargeUser(long userId){
		Boolean temp = stringRedisTemplate.opsForSet().isMember("firstRechargeUser",userId+"");
		if(temp == null){
			return false;
		}
		return temp;
	}
}
